package org.shock.ss.sms.action.system;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.net.URLEncoder;

import org.apache.commons.beanutils.BeanUtils;

import org.shock.ss.sms.bean.Page;
import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.bean.system.Dic;
import org.shock.ss.sms.bean.system.Module;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.utils.ParamsUtil;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.dao.system.impl.DicDaoImpl;
import org.shock.ss.sms.service.system.DicService;
import org.shock.ss.sms.service.system.impl.DicServiceImpl;
import org.shock.ss.sms.action.BaseAction;

/**
 * 实现数据字典功能
 * 
 * @author jile
 * 
 */
public class DicAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DicService ds = new DicServiceImpl((DicDaoImpl) DaoFactory.getDao("DicDao"));
	private List diclist;
	private Dic dic = new Dic();
	private long id;
	private String pcode = "-1";
	private String code;
	private List valuelist = new ArrayList();

	/** *************一级字典操作************* */
	/**
	 * 显示一级字典
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "一级字典列表")
	public String firstDic() throws Exception {

		if (!Util.isEmpty(dic.getCode())) {
			sqladd.append(" and code like '%" + Util.nulltoempty(dic.getCode()) + "%'");
		}
		if (!Util.isEmpty(dic.getValue())) {
			sqladd.append(" and value like '%" + Util.nulltoempty(dic.getValue().trim()) + "%'");
		}

		/** Pager bean 封装 list 和 总数 */
		Pager pager = new Pager();
		/** 分页的参数处理 */
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		/** 获得总数语句 */
		String sqlcount = "select count(d.id) from " + Dic.getTable() + " d  where d.pcode='-1' " + sqladd;
		/** 分页语句 */
		String sql = "select d.id,d.code,d.value,d.pcode,d.min,d.max,d.paixu,d.note from " + Dic.getTable()
				+ " d where d.pcode='-1'" + sqladd;
		/** 获得pager */
		pager = ds.list("desc", "d.paixu", pageno, 200, sqlcount, sql);
		diclist = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), 200, pageno, pagerow, sul);
		pageStr = page.getPageString();
		return SUCCESS;
	}

	@RightAnno(rightNote = "添加一级字典")
	public String addDic() throws Exception {
		if (action.equals("before")) {
			return "before";
		}
		dic.setPcode("-1");
		ds.add(dic);
		jmpUrl = "firstDic.do?pageno=" + pageno;
		message = "添加成功！";
		return SUCCESS;

	}

	/**
	 * 删除系统字典信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "删除一级字典")
	public String deleteDic() throws Exception {
		String sql = "delete from " + Dic.getTable() + " where id=" + dic.getId();
		int subdic = DbTools.getInt("select count(1) from " + Dic.getTable() + " where pcode=(select code from "
				+ Dic.getTable() + " where id=" + dic.getId() + ")");
		if (subdic > 0) {
			throw new RuntimeException("请先删除子项目！");
		}
		ds.del(sql);
		jmpUrl = "firstDic.do?pageno=" + pageno;
		message = "删除成功!";
		return SUCCESS;
	}

	/**
	 * 进入修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "修改一级字典")
	public String updateDic() throws Exception {
		if (action.equals("before")) {
			String sql = "select id,code,value,pcode,max,min,paixu,note from " + Dic.getTable() + " where id="
					+ dic.getId();
			Map map = ds.view(sql);
			BeanUtils.populate(dic, map);
			code = dic.getCode();
			return "before";
		}

		ds.update(dic);
		jmpUrl = "firstDic.do?pageno=" + pageno;
		message = "修改成功!";
		return SUCCESS;

	}

	/**
	 * 修改、添加 字典 id为0 或null时为添加
	 * 
	 * @return
	 * @throws Exception
	 */

	public String saveDic() throws Exception {
		if (dic.getId() == null || dic.getId() == 0) {
			addDic();
		} else {
			updateDic();
		}
		return SUCCESS;
	}

	/** **子字典操作************** */
	/**
	 * 二级字典
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "二级字典列表")
	public String secondDic() throws Exception {
		String sql1 = "select value,code from " + Dic.getTable() + " where pcode='" + code + "' order by paixu desc ";
		valuelist = ds.listAll(sql1);
		if (!Util.isEmpty(dic.getCode())) {
			sqladd.append(" and code like '%" + Util.nulltoempty(dic.getCode()) + "%'");
		}
		if (!Util.isEmpty(dic.getValue())) {
			sqladd.append(" and value like '%" + Util.nulltoempty(dic.getValue().trim()) + "%'");
		}

		/** Pager bean 封装 list 和 总数 */
		Pager pager = new Pager();
		/** 分页的参数处理 */
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		/** 获得总数语句 */
		String sqlcount = "select count(d.id) from " + Dic.getTable() + " d  where d.pcode='" + code + "'" + sqladd;
		/** 分页语句 */
		String sql = "select d.id,d.code,d.value,d.pcode,d.min,d.max,d.paixu,d.note from " + Dic.getTable()
				+ " d where d.pcode='" + code + "'" + sqladd;
		/** 获得pager */
		pager = ds.list("desc", "d.paixu", pageno, rownum, sqlcount, sql);
		diclist = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), rownum, pageno, pagerow, sul);
		pageStr = page.getPageString();
		return SUCCESS;
	}

	/**
	 * 进入添加子字典页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "添加二级字典")
	public String addDicSecond() throws Exception {
		if (action.equals("before")) {
			dic.setPcode(pcode);
			return "before";
		}
		ds.add(dic);
		code = URLEncoder.encode(dic.getPcode(), "utf-8");
		jmpUrl = "secondDic.do?code=" + dic.getPcode() + "&amp;pageno=" + pageno;
		message = "添加成功";
		return SUCCESS;

	}

	/**
	 * 删除系统子字典信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "删除二级字典")
	public String deleteDicSecond() throws Exception {
		int subdic = DbTools.getInt("select count(1) from " + Dic.getTable() + " where pcode=(select code from "
				+ Dic.getTable() + " where id=" + dic.getId() + ")");
		if (subdic > 0) {
			throw new RuntimeException("请先删除子项目！");
		}
		String sql = "delete from " + Dic.getTable() + " where id =" + dic.getId();
		ds.del(sql);
		jmpUrl = "secondDic.do?code=" + code + "&amp;pageno=" + pageno;
		// System.out.println(jmpUrl);
		message = "删除成功";
		return SUCCESS;
	}

	/**
	 * 进入修改子字典 页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "修改二级字典")
	public String updateDicSecond() throws Exception {
		if (action.equals("before")) {
			String sql = "select id,code,value,pcode,max,min,paixu,note from " + Dic.getTable() + " where id="
					+ dic.getId();
			Map map = ds.view(sql);
			BeanUtils.populate(dic, map);
			code = dic.getCode();
			return "before";
		}

		ds.update(dic);
		code = URLEncoder.encode(dic.getPcode(), "utf-8");
		jmpUrl = "secondDic.do?code=" + dic.getPcode() + "&amp;pageno=" + pageno;
		message = "修改成功";
		return SUCCESS;
	}

	/**
	 * 修改、添加 子字典 id为0 或null时为添加
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveSecondDic() throws Exception {
		if (dic.getId() == null || dic.getId() == 0) {
			addDicSecond();
		} else {
			updateDicSecond();
		}
		return SUCCESS;
	}

	/** **三级字典操作************** */
	/**
	 * 三级字典
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "三级字典列表")
	public String thirdDic() throws Exception {
		String sql1 = "select value,code from " + Dic.getTable() + " connect by code=prior pcode start with code='"
				+ code + "' order by id ";
		valuelist = ds.listAll(sql1);
		if (!Util.isEmpty(dic.getCode())) {
			sqladd.append(" and code like '%" + Util.nulltoempty(dic.getCode()) + "%'");
		}
		if (!Util.isEmpty(dic.getValue())) {
			sqladd.append(" and value like '%" + Util.nulltoempty(dic.getValue().trim()) + "%'");
		}

		/** Pager bean 封装 list 和 总数 */
		Pager pager = new Pager();
		/** 分页的参数处理 */
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		/** 获得总数语句 */
		String sqlcount = "select count(d.id) from " + Dic.getTable() + " d  where d.pcode='" + code + "'" + sqladd;
		/** 分页语句 */
		String sql = "select d.id,d.code,d.value,d.pcode,d.min,d.max,d.paixu,d.note from " + Dic.getTable()
				+ " d where d.pcode='" + code + "'" + sqladd;
		/** 获得pager */
		pager = ds.list("desc", "d.id", pageno, rownum, sqlcount, sql);
		diclist = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), rownum, pageno, pagerow, sul);
		pageStr = page.getPageString();
		return SUCCESS;
	}

	/**
	 * 进入添加子字典页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "添加三级字典")
	public String addDicThird() throws Exception {
		if (action.equals("before")) {
			dic.setPcode(pcode);
			return "before";
		}
		ds.add(dic);
		code = URLEncoder.encode(dic.getPcode(), "utf-8");
		jmpUrl = "thirdDic.do?code=" + dic.getPcode() + "&amp;pageno=" + pageno;
		message = "添加成功";
		return SUCCESS;

	}

	/**
	 * 删除系统子字典信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "删除三级字典")
	public String deleteDicThird() throws Exception {
		int subdic = DbTools.getInt("select count(1) from " + Dic.getTable() + " where pcode=(select code from "
				+ Dic.getTable() + " where id=" + dic.getId() + ")");
		if (subdic >0) {
			throw new RuntimeException("请先删除子项目！");
		}
		String sql = "delete from " + Dic.getTable() + " where id =" + dic.getId() ;
		ds.del(sql);
		jmpUrl = "thirdDic.do?code=" + dic.getPcode() + "&amp;pageno=" + pageno;
		message = "删除成功";
		return SUCCESS;
	}

	/**
	 * 进入修改子字典 页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RightAnno(rightNote = "修改三级字典")
	public String updateDicThird() throws Exception {
		if (action.equals("before")) {
			String sql = "select id,code,value,pcode,max,min,paixu,note from " + Dic.getTable() + " where id="
					+ dic.getId();
			Map map = ds.view(sql);
			BeanUtils.populate(dic, map);
			code = dic.getCode();
			return "before";
		}
		ds.update(dic);
		code = URLEncoder.encode(dic.getPcode(), "utf-8");
		jmpUrl = "thirdDic.do?code=" + dic.getPcode() + "&amp;pageno=" + pageno;
		message = "修改成功";
		return SUCCESS;
	}

	/**
	 * 修改、添加 子字典 id为0 或null时为添加
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveThirdDic() throws Exception {
		if (dic.getId() == null || dic.getId() == 0) {
			addDicThird();
		} else {
			updateDicThird();
		}
		return SUCCESS;
	}

	public List getDiclist() {
		return diclist;
	}

	public void setDiclist(List diclist) {
		this.diclist = diclist;
	}

	public DicService getDs() {
		return ds;
	}

	public void setDs(DicService ds) {
		this.ds = ds;
	}

	public Dic getDic() {
		return dic;
	}

	public void setDic(Dic dic) {
		this.dic = dic;
	}

	public String getPcode() {
		return pcode;
	}

	public void setPcode(String pcode) {
		this.pcode = pcode;
	}

	public List<String> getValuelist() {
		return valuelist;
	}

	public void setValuelist(List<String> valuelist) {
		this.valuelist = valuelist;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

}
